package com.example.rbac.controller;

import com.example.rbac.common.ApplicationContextHelper;
import com.example.rbac.common.web.JsonData;
import com.example.rbac.dao.SysAclModuleMapper;
import com.example.rbac.model.SysAclModule;
import com.example.rbac.model.vo.TestVO;
import com.example.rbac.util.JsonMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author XieLongzhen
 * @date 2018/12/14 21:09
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController extends BaseController{

    @GetMapping("/hello.json")
    public JsonData hello() {
        log.info("hello");
        return JsonData.success("hello rbac");
    }

    @GetMapping("/validate.json")
    public JsonData validate(@Valid TestVO vo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            generateValidateFailedResponse(bindingResult);
        }
        log.info("validation");
        return JsonData.success("test validate");
    }

    @GetMapping("/applicationContext.json")
    public JsonData applicationContext() {
        SysAclModuleMapper moduleMapper = ApplicationContextHelper.popBean(SysAclModuleMapper.class);
        SysAclModule module = moduleMapper.selectByPrimaryKey(1);
        log.info(JsonMapper.obj2Str(module));
        return JsonData.success("test context");
    }
}
